// 云函数入口文件
const cloud = require('wx-server-sdk')
let superagent = require('superagent') // 发起请求获取页面html代码
let cheerio = require('cheerio') // 读取dom结构中的标签
let charset = require('superagent-charset') // 解决乱码
charset(superagent)
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
    let server = `https://m.bige7.com/${event.url}/list.html`
    const result = await  superagent.get(server)// 拿到页面的html
    const $ = cheerio.load(result.text)

    // 分页章节
    let currentPage = event.currentPage // 第几页

    let pageData = {}
    let pageSection = []
    let pageList = $('.book_last').find('dd')
    pageData['totalPage'] = pageList.length / 20 // 当前这本小说能形成多少页
    let len = (currentPage - 1)*20
    for(let i = len + 1; i < 21 + len;i++) {
        let obj = {}
        obj['url'] = $(pageList[i]).find('a').attr('href')
        obj['sectionName'] = $(pageList[i]).find('a').text()
        pageSection.push(obj)
    }
    pageData['currentPage'] = pageSection
    return {
        pageData
    }
}